package com.novacloud.fresh.authentication.action;

import org.jasig.cas.authentication.Credential;
import org.jasig.cas.web.support.WebUtils;
import org.springframework.binding.message.MessageBuilder;
import org.springframework.binding.message.MessageContext;
import org.springframework.webflow.execution.RequestContext;

import javax.servlet.http.HttpSession;

/**
 * @author <a href="mailto:huzhanjun@novacloud.com">huzj</a>
 * @version 1.0
 * @date 2016/4/27 14:15
 */
public class SecurityCodeAction {

    public final String submit(final RequestContext context, final Credential credential,
                               final MessageContext messageContext)
            throws Exception {
        String securitycode = context.getRequestParameters().get("securitycode");
        HttpSession session = WebUtils.getHttpServletRequest(context).getSession();
        String code = (String)session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
        if (securitycode.equalsIgnoreCase(code)) {
            return "success";
        } else {
            MessageBuilder msgBuilder = new MessageBuilder();
            msgBuilder.defaultText("Verification code is not correct！");
            messageContext.addMessage(msgBuilder.error().build());
            return "error";
        }
    }
}
